home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: pstaite@vnet.ibm.com
- Newsgroups: comp.lang.c++
- Subject: Re: Binaries
- Date: 4 Jan 1996 14:45:37 GMT
- Organization: IBM OS/2 Device Driver Development Rochester, MN
- Message-ID: <4cgp6h$15vt@locutus.rchland.ibm.com>
- References: <peterf.58.00174FE2@gears.efn.org>
- Reply-To: pstaite@vnet.ibm.com
- NNTP-Posting-Host: warpone.rchland.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <peterf.58.00174FE2@gears.efn.org>, peterf@gears.efn.org (Peter F.) writes:
- >
- >#include <stdio.h>
- >
- >void main(void)
- > {
- > FILE *in, *out;
- >
- > if ((in = fopen("C:\\car.pcx", "rb"))== NULL)
- > {
- > fprintf(stderr, "Cannot open input file.\n");
- > }
- >
- > if ((out = fopen("C:\\temp\\car.pcx", "wb"))== NULL)
- > {
- > fprintf(stderr, "Cannot open output file.\n");
- > }
- >
- > while (!feof(in))
- > { fputc(fgetc(in), out); }
-
- The problem is right here with the copy loop. The feof() function/macro
- tests for the EOF status bit. However, this bit isn't set until _after_
- a read is attempted beyond the end of the file. The EOF state is _not_
- set merely by reading the last byte, you must try to read one more...
-
- Therefore, to terminate the loop fgetc() must read one extra byte and
- fail at it. However, by this time you've already tried to write that
- byte. (I'll bet it is 0x1a -- EOF -- that is getting appended to your
- files)
-
- Try this, declare an int variable c up with the FILE*'s:
-
- int c;
-
- Then code your loop as:
-
- while( ( c = fgetc( in ) ) != EOF )
- fputc( c, out );
-
- This way, you check to make sure that the read succeeded before doing
- the write.
-
- Of course, this _is_ a C++ group, why don't you try:
-
- #include<fstream.h>
-
- int main() {
- ifstream infile( "C:\\car.pcx", ios::bin ); // may be ios::binary
- ofstream outfile( "C:\\temp\\car.pcx", ios::bin );
-
- if( ! infile )
- cerr << "Cannot open input file." << endl;
- if( ! outfile )
- cerr << "Cannot open output file." << endl;
- outfile << infile.rdbuf();
- return 0; }
-
- >Also if anyone knows how to access Dos commands like copy or move from C++
- >this could help also. I use BC++ 3.1.
-
- Most C/C++ libraries support the system() command, you could also look
- at spawnl() and/or execl(). Probably the simplest would be to just
- enter:
-
- system( "copy c:\\car.pcx c:\\temp\\car.pcx" );
-
- I think you need to #include dos.h or stdlib.h to get the system()
- prototype.
-
- >P.S. -- Anyone know how the copy command works, like how it keeps the same
- >date, time, file size, of the target file you copied from.
-
- That's not really a standard opperation (C/C++). There is a DOS call
- you can make to get/set a file's date. You'll have to check a DOS
- programming reference to find it. (or email me and I'll dig it up...)
-
-
- Phil Staite, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-
-